package com.tiancheng.trade.order.controller.parallel;

import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.commom.core.exception.error.BusinessErrorInfoEnum;
import com.tiancheng.trade.order.service.OrderBatchPayService;
import com.tiancheng.trade.order.service.OrderParallelService;
import com.tiancheng.trade.order.vo.parallel.*;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Map;

/**
 * 新、老系统并行，提供给老系统兼容的订单接口
 *
 * @author kellen
 * @date 2024/6/5
 */
@Slf4j
@RestController
@RequestMapping("/api/parallel")
@Validated
public class OrderParallelApiController {

    @Resource
    private OrderParallelService orderParallelService;
    @Resource
    private OrderBatchPayService orderBatchPayService;

    /**
     * 查询用户订单
     * @param orderQueryVO
     * @return
     */
    @PostMapping(value = "/findByUser", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result findByUser(@RequestBody OrderQueryVO orderQueryVO) {
        if(orderQueryVO.getSize() == 0) {
            orderQueryVO.setSize(10);
        }
        return Result.success(orderParallelService.findByUser(orderQueryVO));
    }

    /**
     * 初始化收银台
     * @param cashierVO
     * @return
     */
    @PostMapping(value = "/cashier", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result cashier(@RequestBody @Valid CashierParallelVO cashierVO) {
        // 校验订单，不允许批次订单和其他订单一起付款
        final long batchCount= Arrays.stream(cashierVO.getOrderIds()).filter(o -> o.startsWith("B")).distinct().count();
        final long orderCount = Arrays.stream(cashierVO.getOrderIds()).filter(o -> !o.startsWith("B")).distinct().count();
        if(orderCount==1 && batchCount==0){
            return Result.success(orderParallelService.initCashier(cashierVO));
        } else if(orderCount==0 && batchCount==1){
            return Result.success(orderBatchPayService.initCashier(cashierVO));
        }else{
            BusinessErrorInfoEnum.ORDER_CASHIER_MERGE_NOT_SUPPORT.assertFail();
        }
        return Result.fail(BusinessErrorInfoEnum.ORDER_CASHIER_MERGE_NOT_SUPPORT.getCode(),BusinessErrorInfoEnum.ORDER_CASHIER_MERGE_NOT_SUPPORT.getMessage());
    }

    /**
     * 支付
     * @param orderPayVO
     * @return
     */
    @PostMapping(value = "/pay", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result pay(@RequestBody @Valid OrderPayParallelVO orderPayVO) {
        // 校验订单，不允许批次订单和其他订单一起付款
        final long batchCount= Arrays.stream(orderPayVO.getOrderIds()).filter(o -> o.startsWith("B")).distinct().count();
        final long orderCount = Arrays.stream(orderPayVO.getOrderIds()).filter(o -> !o.startsWith("B")).distinct().count();
        if(orderCount==1 && batchCount==0){
            return Result.success(orderParallelService.pay(orderPayVO));
        } else if(orderCount==0 &&  batchCount==1){
            return Result.success(orderBatchPayService.pay(orderPayVO));
        }else{
            BusinessErrorInfoEnum.ORDER_PAY_NOT_ALLOW_MERGE_ERROR.assertFail();
        }
        return Result.fail(BusinessErrorInfoEnum.ORDER_PAY_NOT_ALLOW_MERGE_ERROR.getCode(),BusinessErrorInfoEnum.ORDER_PAY_NOT_ALLOW_MERGE_ERROR.getMessage());
    }

    /**
     * 支付
     * @param appleInAppPayVO
     * @return
     */
    @PostMapping(value = "/appleInAppPay", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result appleInAppPay(@RequestBody @Valid AppleInAppPayVO appleInAppPayVO) {
        return Result.success(orderParallelService.appleInAppPay(appleInAppPayVO));
    }


    /**
     * 支付
     * @param orderPayVO
     * @return
     */
    @PostMapping(value = "/deductionPay", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result deductionPay(@RequestBody @Valid OrderDeductionPayParallelVO orderPayVO) {
        return Result.success(orderParallelService.deductionPay(orderPayVO));
    }


    /**
     * 获取订单支付状态
     * @param params
     * @return
     */
    @PostMapping(value = "/getPayStatus", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result getPayStatus(@RequestBody Map<String, String> params) {
        // 校验订单，不允许批次订单和其他订单一起付款
        String orderId=params.get("order_id");
        if(!orderId.startsWith("B")){
            return Result.success(orderParallelService.getPayStatus(params.get("uid"), params.get("order_id")));
        } else {
            return Result.success(orderBatchPayService.getPayStatus(params.get("uid"), params.get("order_id")));
        }
    }

    /**
     * 查询用户订单
     * @param orderQueryVO
     * @return
     */
    @PostMapping(value = "/findByUserNew", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result findByUserNew(@RequestBody OrderQueryVO orderQueryVO) {
        if(orderQueryVO.getSize() == 0) {
            orderQueryVO.setSize(10);
        }
        return Result.success(orderParallelService.findByUserNew(orderQueryVO));
    }

}
